'use strict';

const AdminBaseController = require('../base');
const jwt = require('jsonwebtoken');
const { CommonUserTypeEnum } = require('../../utils/enum');
class SystemUserController extends AdminBaseController {
  static get serviceName() {
    return 'system.systemUser';
  }
  // 后台登录
  async login() {
    const { ctx } = this;
    const result = await this.service.login(ctx.request.body);
    // token
    console.log('ctx.request.body.type',ctx.request.body.type)
    const token = jwt.sign({ ...result, type: ctx.request.body.type }, ctx.app.config.keys);
    ctx.success(token);
  }
  // 获取用户信息以及权限菜单
  async getUserInfo() {
    try {
      const { ctx } = this;
      // 判断当前登录的鉴权用户token中解析到上下文
      const user = ctx.user;
      console.log(user)
      if (user.type === CommonUserTypeEnum.SYSTEM_USER) {
        const menus = await this.service.getMenu();
        ctx.success({
          user: {
            ...user,
            type: CommonUserTypeEnum.SYSTEM_USER
          },
          menus
        });
      } else {
        ctx.success({
          user: {
           ...user,
            type: CommonUserTypeEnum.SHOP_USER
          },
          menus: []
        })
      }

    } catch (e) {
      console.log(e)
    }
  }
}

module.exports = SystemUserController;
